﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MLY.Bll;

namespace MLY.LTR
{
  public partial class EditMessage : BasePage
  {
    clsMessage wrapmsg;
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Expires = 0;
      if (!Page.IsPostBack)
      {
        ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];
        int OrgId = int.Parse(Session["OrganizationId"].ToString());
        int MsgId;

        if (Int32.TryParse(Request.QueryString["MsgId"], out MsgId))
        {
          if (MsgId != -1)
          {
            wrapmsg = new clsMessage(MsgId);
            this.txtMsgTitle.Text = wrapmsg.Data.MsgTitle;
            this.txtMsgText.Text = wrapmsg.Data.MsgText;
            this.ckDisplay.Checked = (bool)wrapmsg.Data.DisplayMsg;
            this.calExpirationDate.Text = wrapmsg.Data.ExpirationDate.Value.ToShortDateString();
            CheckBoxListContractors.DataSource = clsMessageRelationsContacts.GetOrgMessageList(OrgId, MsgId);
            CheckBoxListContractors.DataBind();
            if (CheckBoxListContractors.Items.Count > 0)
            {
              lblContractorNames.Visible = true;
            }
            else
            {
              lblContractorNames.Visible = false;
            }
            int idx = 0;
            foreach (clsMessageRelationsContacts tmp in clsMessageRelationsContacts.GetOrgMessageList(OrgId, MsgId))
            {
              if (tmp.GetDisplay)
              {
                CheckBoxListContractors.Items[idx].Selected = true;
              }
              idx++;
            }
          }
          else
          {
            //new
            wrapmsg = new clsMessage();
            CheckBoxListContractors.DataSource = clsOrganizationRelation.GetOrgContractorAndClients(OrgId);
            CheckBoxListContractors.DataBind();
            calExpirationDate.Text = DateTime.Today.AddMonths(1).ToShortDateString();
          }
        }
      }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
      Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext();
      int MsgId = int.Parse(Request.QueryString["MsgId"].ToString());

      if (MsgId != -1)
        wrapmsg = new clsMessage(MsgId);
      else 
        wrapmsg = new clsMessage();

      wrapmsg.Data.MsgTitle = this.txtMsgTitle.Text;
      wrapmsg.Data.MsgText = this.txtMsgText.Text;
      wrapmsg.Data.DisplayMsg = this.ckDisplay.Checked;
      DateTime? expirationDate = Utils.ParseDate(this.calExpirationDate.Text);
      if (expirationDate != null)
      {
        wrapmsg.Data.ExpirationDate = expirationDate;
      }
      else wrapmsg.Data.ExpirationDate = new DateTime(2099, 1, 1);

      wrapmsg.Data.OrganizationId = int.Parse(Session["OrganizationId"].ToString());
      wrapmsg.Data.UserId = int.Parse(Session["UserId"].ToString());
      wrapmsg.Save();

      MsgId = wrapmsg.Data.MsgId;

      // First delete all message relations in table TableMessageRelation                    
      clsMessage.DeleteMessageRelations(MsgId);

      // Insert all organizations that are checked
      for (int idx = 0; idx < CheckBoxListContractors.Items.Count; idx++)
      {
        if (CheckBoxListContractors.Items[idx].Selected == true)
        {
          clsMessageRelation msg = new clsMessageRelation(MsgId, new clsMessageRelationsContacts(int.Parse(CheckBoxListContractors.Items[idx].Value), CheckBoxListContractors.Items[idx].Text.ToString()));
          msg.Data.UpdatedBy = int.Parse(Session["UserId"].ToString());
          msg.Save();
        }
      }
      Response.RedirectTo((string)ViewState["Referer"]);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
      Response.RedirectTo((string)ViewState["Referer"]);
    }
  }
}
